﻿using System;
using System.Globalization;
using App_Code.Helpers;
using VisualCron;

namespace App_Code.Objects
{
    /// <summary>
    /// Summary description for JobDTO
    /// </summary>
    public class JobDTO
    {
        public string Active { get; set; }

        public string Id { get; set; }

        public string ServerId { get; set; }

        public string JobName { get; set; }

        public string Description { get; set; }

        public DateTime DateCreated { get; set; }

        public string DateCreatedStr
        {
            get
            {
                return DateCreated.ToString(PageHelper.CurrentUserDateTimeFormat());
            }
        }

        public DateTime LastRun { get; set; }

        public string LastRunStr
        {
            get { return LastRun.ToString(PageHelper.CurrentUserDateTimeFormat()); }
        }


        public DateTime NextRun { get; set; }

        public string NextRunStr
        {
            get
            {
                return NextRun.ToString(PageHelper.CurrentUserDateTimeFormat());
            }
        }
        public string Result { get; set; }

        public string Status { get; set; }

        public int TaskCount { get; set; }

        #region Constructor

        public JobDTO(string serverId, VisualCron.JobClass job)
        {
            ServerId = serverId;
            if (job != null)
            {
                Active = job.Stats.Active ? "Active" : "Inactive";
                Id = job.Id;
                JobName = job.Name;
                DateCreated = job.Stats.DateCreated;
                Description = job.Description;
                LastRun = job.Stats.DateLastExecution;
                NextRun = job.Stats.DateNextExecution;
                Result = (job.Stats.Status == JobStatsClass.StatusT.Running)
                             ? "Running"
                             : (LastRun == DateTime.MinValue)
                                   ? "Success"
                                   : (job.Stats.ExitCode == 0) ? "Success" : "Failure";

                Status = job.Stats.Status.ToString();
                TaskCount = job.Tasks.Count;
            }
        }
        #endregion
    }
}